package com.string.actortv.util;

import android.util.Log;
import com.string.actortv.dto.MovieInfo;
import com.string.actortv.dto.MovieList;
import com.string.actortv.dto.MovieTypeList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import static com.string.actortv.util.CacheHelper.CACHE;

public class QmmkUtil {

    private static final String BASE_URL = "https://www.headquarterhotel.cn";
    private static final String BASE_PAGE_URL = "https://www.headquarterhotel.cn/vodsearch/-------------.html?wd=%E5%A5%87%E5%A6%99%E8%90%8C%E5%8F%AF";

    private static String getBaseTypeUrl(String action) {
        return "https://www.jxtcd.com/index.php?s=home-vod-type-id-" + action + "-picm-1-p-1";
    }

    public interface MovieTypeCallBack {
        void execute(List<MovieTypeList> movieTypeList);
    }

    /**
     * @param callBack
     */
    public static void getMovieTypeAsync(MovieTypeCallBack callBack) {
        List<MovieTypeList> movieTypeLists = new ArrayList<>();
        movieTypeLists.add(new MovieTypeList("全部", "0"));
        callBack.execute(movieTypeLists);
    }

    public interface MovieUrlCallBack {
        void execute(String playUrl);
    }

    public static void getMovieUrlAsync(String pageUrl, MovieUrlCallBack callBack) {
        MyHttpUtil.getAsync(pageUrl, html -> {
            String playUrl = html.substring(html.indexOf("?url=") + 5);
            callBack.execute(playUrl);
        }, null);
    }

    public interface MovieListCallBack {
        void execute(List<MovieList> movieList);
    }

    public static void getMovieListAsync(int pageNo, String searchStr,
                                         String type, String action,
                                         MovieListCallBack movieListCallBack) {
        if (CACHE.containsKey(BASE_PAGE_URL)
                && CACHE.get(BASE_PAGE_URL) != null) {
            getList(BASE_PAGE_URL, searchStr, CACHE.get(BASE_PAGE_URL).toString(), movieListCallBack);
        } else {
            MyHttpUtil.getAsync(BASE_PAGE_URL, result -> getList(BASE_PAGE_URL, searchStr, result, movieListCallBack), null);
        }
    }

    private static void getList(String pageUrl, String searchStr,
                                String html, MovieListCallBack movieListCallBack) {
        if ("".equals(html)) {
            Log.d("JxtcdGetList", html);
            movieListCallBack.execute(null);
            return;
        }
        CACHE.put(pageUrl, html);
        Document document = Jsoup.parse(html);
        List<MovieList> movieList = new ArrayList<>();
        //获取当前电影列表
        Elements movieElements;
        movieElements = document
                .getElementsByClass("row")
                .get(0).getElementsByClass("cover");
        if (movieElements != null) {
            for (Element movieElement : movieElements) {
                String name;
                String img;
                String url;
                name = movieElement.attr("alt");
                img = movieElement.attr("data-original");
                url = BASE_URL + movieElement.attr("href");
                if (img.startsWith("//")) {
                    img = "https:" + img;
                } else if (img.startsWith("/")) {
                    img = BASE_URL + img;
                }
                movieList.add(new MovieList(name, url, img));
            }
        }
        movieListCallBack.execute(movieList);
    }

    public interface MovieInfoCallBack {
        void execute(MovieInfo movieInfo);
    }

    public static void getMovieInfoAsync(String href, String name, MovieInfoCallBack movieInfoCallBack) {
        //获取电影集数列表
        if (CACHE.containsKey(href)
                && CACHE.get(href) != null) {

            getInfo(name, href, CACHE.get(href).toString(), movieInfoCallBack);
        } else {
            MyHttpUtil.getAsync(href, movieHtml -> {
                if ("".equals(movieHtml)) {
                    movieInfoCallBack.execute(null);
                    return;
                }
                CACHE.put(href, movieHtml);

                getInfo(name, href, movieHtml, movieInfoCallBack);
            }, null);
        }
    }

    private static void getInfo(String name, String href, String movieHtml, MovieInfoCallBack movieInfoCallBack) {
        Document document = Jsoup.parse(movieHtml);
        Elements listElements = document
                .getElementById("playlist1")
                .getElementsByTag("a");
        List<MovieInfo.SeasonList> seasonList = new ArrayList<>();
        if (listElements != null && !listElements.isEmpty()) {
            for (Element seasonElement : listElements) {
                String seasonName = seasonElement.text();
                String playJs = seasonElement.attr("onclick");
                String sid = playJs.substring(5, playJs.indexOf(","));
                String nid = playJs.substring(playJs.indexOf(",") + 1, playJs.indexOf(")"));
                String url = href + "?sid=" + sid + "&nid=" + nid;

                seasonList.add(new MovieInfo.SeasonList(seasonName, url));
            }
        }
        movieInfoCallBack.execute(new MovieInfo(name, "", "", seasonList));
    }

}
